const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
const cors = require('koa2-cors')

const errorHandle = require('./error_handle')
const useRoutes = require('../router/index')

const app = new Koa()

app.use(
  cors({
    origin: function (ctx) {
    //设置允许来自指定域名请求
    return "*"; // 允许来自所有域名请求
    },
    maxAge: 5, //指定本次预检请求的有效期，单位为秒。
    credentials: true, //是否允许发送Cookie
    allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], //设置所允许的HTTP请求方法
    allowHeaders: ["Content-Type", "Authorization", "Accept"], //设置服务器支持的所有头信息字段
    exposeHeaders: ["WWW-Authenticate", "Server-Authorization"], //设置获取其他自定义字段
  })
);

app.use(
  bodyParser({
    enableTypes: ["json", "form", "text"],
    multipart: true,
    formidable: {
    maxFileSize: 200 * 1024 * 1024,
    },
  })
);

app.use(bodyParser())
useRoutes(app)

app.on('error',errorHandle)

module.exports = app